package com.xxxx.yoyo.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.xxxx.yoyo.bean.Goods;
import com.xxxx.yoyo.bean.Type;
import com.xxxx.yoyo.service.IGoodsService;
import com.xxxx.yoyo.service.ITypeService;
import com.xxxx.yoyo.service.impl.GoodsServiceImpl;
import com.xxxx.yoyo.service.impl.TypeServiceImpl;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/index/index")//  localhost:8080/项目名/index/index
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private IGoodsService goodsService = new GoodsServiceImpl();
	private ITypeService typeService = new TypeServiceImpl();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		
		//设置导航条上的 [首页] 被选中
		request.setAttribute("flag", 1);
		
		//获取 [宠物分类] 列表
		List<Type> typeList = typeService.getTypeList();
		//把typeList存入request对象, 供/index/index.jsp页面使用
		request.setAttribute("typeList", typeList);
		
		//获取 [今日推荐] 宠物列表
		List<Goods> goodsList = goodsService.getGoodsListByHotType(1);
		//把goodsList存入request, 供/index/index.jsp页面使用
		request.setAttribute("top1List", goodsList);
		
		//获取 [热销推荐] 宠物列表
		List<Goods> goodsList2 = goodsService.getGoodsListByHotType(2);
		//把goodsList2存入request, 供/index/index.jsp页面使用
		request.setAttribute("top2List", goodsList2);
		
		//获取 [新品推荐] 宠物列表
		List<Goods> goodsList3 = goodsService.getGoodsListByHotType(3);
		//把goodsList3存入request, 供/index/index.jsp页面使用
		request.setAttribute("top3List", goodsList3);
		
		//请求转发,转发到 /index/index.jsp
		request.getRequestDispatcher("/index/index.jsp").forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		
		doGet(request, response);
	}
}
